package com.puboot.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.Pagination;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.BizCourse;
import com.puboot.module.admin.model.BizCoursePlan;
import com.puboot.module.admin.model.BizTeacher;
import com.puboot.module.admin.model.BizTeaching;
import com.puboot.module.admin.service.BizCoursePlanService;
import com.puboot.module.admin.service.BizCourseService;
import com.puboot.module.admin.service.BizTeacherService;
import com.puboot.module.admin.service.BizTeachingService;
import com.puboot.module.admin.vo.TeachingConditionVo;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ltc
 * @version 1.0
 * @date 2020/10/4 11:06
 */
@Controller
@RequestMapping("teaching")
@AllArgsConstructor
public class BizTeachingController {
    //授课
    private final BizTeachingService bizTeachingService;
    //课程
    private final BizCourseService bizCourseService;
    //课程安排
    private final BizCoursePlanService bIzCoursePlanService;
    //老师
    private final BizTeacherService bizTeacherService;


    @GetMapping("/add")
    public String add() {
        return CoreConst.ADMIN_PREFIX + "teaching/form";
    }




    @PostMapping("list")
    @ResponseBody
    public PageResultVo loadTeachings(TeachingConditionVo vo, Integer pageNumber, Integer pageSize) {
        IPage<BizTeaching> page = new Pagination<>(pageNumber, pageSize);
        List<BizTeaching> teachingList = bizTeachingService.PageTeachingsConditionVo(page, vo);
        return ResultUtil.table(teachingList, page.getTotal());
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(BizTeaching bizTeaching) {
        bizTeaching.setStatus(1);
        Date date = new Date();
        bizTeaching.setCreateTime(date);
        bizTeaching.setUpdateTime(date);
        BizCourse bizCourse = bizCourseService.getById(bizTeaching.getCourseId());
        if (bizCourse.getIsChild()==2){//儿童科目
            bizTeaching.setChildAge(null);
            bizTeaching.setChildHeight(0D);
            bizTeaching.setChildIdcard("");
            bizTeaching.setChildWeight(0D);
            bizTeaching.setChildSex(null);
            bizTeaching.setChildName("");
        }
        boolean i = bizTeachingService.save(bizTeaching);
        if (i)
            return ResultUtil.success("新增报名授课信息成功");
        return ResultUtil.error("新增报名授课信息失败");
    }

    @GetMapping("/edit")
    public String edit(Model model, Integer id) {
        //查询报名授课信息
        BizTeaching bizTeaching = bizTeachingService.getById(id);

        BizCourse bizCourse = bizCourseService.getById(bizTeaching.getCourseId());
        bizTeaching.setCourses(bizCourse);//课程
        if (null !=bizCourse.getIsChild()&&bizCourse.getIsChild()==2){//儿童科目
            bizTeaching.setChildAge(null);
            bizTeaching.setChildHeight(0D);
            bizTeaching.setChildIdcard("");
            bizTeaching.setChildWeight(0D);
            bizTeaching.setChildSex(null);
            bizTeaching.setChildName("");
        }

        BizTeacher bizTeacher = bizTeacherService.getById(bizTeaching.getTeacherId());
        bizTeaching.setTeachers(bizTeacher);//老师

        BizCoursePlan bizCoursePlan = bIzCoursePlanService.getById(bizTeaching.getCoursePlanId());
        bizTeaching.setCoursePlans(bizCoursePlan);//课程安排

        model.addAttribute("teaching", bizTeaching);
        return CoreConst.ADMIN_PREFIX + "teaching/form";
    }

    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo edit(BizTeaching bizTeaching) {
        bizTeaching.setUpdateTime(new Date());
        boolean i = bizTeachingService.updateById(bizTeaching);
        if (i)
            return ResultUtil.success("编辑报名授课信息成功");
        return ResultUtil.error("编辑报名授课信息失败");

    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo delete(Integer id) {
        boolean i = bizTeachingService.removeById(id);
        if (i)
            return ResultUtil.success("删除报名授课信息成功");
        return ResultUtil.error("删除报名授课信息失败");

    }

    @PostMapping("/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        int i = bizTeachingService.deleteBatch(ids);
        if (i > 0)
            return ResultUtil.success("删除报名授课信息成功");
        return ResultUtil.error("删除报名授课信息失败");
    }

}
